# -*- coding: utf-8 -*-
# @Time : 2019/2/19 10:55
# @Author : Administrator
# @File : monkey_simple.py
# @Project : Monkey测试
# @title :  笨猴子
import random
import os
import pykeyboard
import pymouse
import time


class MonkeySimple():
    # 初始化鼠标对象和键盘对象
    def __init__(self):
        self.pykeyboard = pykeyboard.PyKeyboard()
        self.pymouse = pymouse.PyMouse()

    # 获取随机坐标
    def rand_pos(self):
        x = random.randint(0,1440)
        y = random.randint(0,900)
        return x,y

    # 执行随机坐标左键单击
    def rand_click(self):
        x,y = self.rand_pos()
        self.pymouse.move(x,y)
        self.pymouse.click(x, y, button=1, n=1)
        time.sleep(1)
        print("在坐标(%d,%d)处执行左键单击操作"%(x,y))

    # 执行随机坐标左键双击
    def rand_double_click(self):
        x, y = self.rand_pos()
        self.pymouse.move(x, y)
        self.pymouse.click(x, y, button=1, n=2)
        time.sleep(1)
        print("在坐标(%d,%d)处执行左键双击操作" % (x, y))

    # 执行随机坐标右键单击
    def rand_right_click(self):
        x,y = self.rand_pos()
        self.pymouse.move(x,y)
        self.pymouse.click(x, y, button=2, n=1)
        time.sleep(1)
        print("在坐标(%d,%d)处执行右键单击操作" % (x, y))

    # 执行下拉框选择操作
    def rand_select(self,count):
        x,y = self.rand_pos()
        self.pymouse.click(x,y)
        for i in range(0,count):
            self.pykeyboard.press_key(self.pykeyboard.down_key)
            self.pykeyboard.release_key(self.pykeyboard.down_key)
        self.pykeyboard.press_key(self.pykeyboard.enter_key)
        self.pykeyboard.release_key(self.pykeyboard.enter_key)
        time.sleep(1)
        print("在坐标(%d,%d)处执行下拉框选择操作" % (x, y))

    # 执行随机输入操作
    def rand_input(self):
        x,y = self.rand_pos()
        self.pymouse.move(x,y)
        self.pymouse.click(x,y)
        list_str = ['hello','world','模块级','monkey','健康']
        rand_str = random.choice(list_str)
        self.pykeyboard.type_string(rand_str)
        time.sleep(1)
        print("在坐标(%d,%d)处执行输入'%s'操作" % (x, y,rand_str))

    # 执行随机按键操作
    def rand_key(self):
        x,y = self.rand_pos()
        self.pymouse.move(x,y)
        list_key = [self.pykeyboard.enter_key,self.pykeyboard.down_key,self.pykeyboard.alt_key,self.pykeyboard.up_key]
        random_key = random.choice(list_key)
        self.pykeyboard.press_key(random_key)
        self.pykeyboard.release_key(random_key)
        time.sleep(1)
        print("在坐标(%d,%d)处执行按键'%s'操作" % (x, y, random_key))

    # 定义随机规则
    def start(self,count,n=2):
        for i in range(count):
            seed = random.randint(0,100)
            if seed<20:
                self.rand_key()
            elif seed<40:
                self.rand_select(n)
            elif seed<55:
                self.rand_right_click()
            elif seed<70:
                self.rand_click()
            elif seed<90:
                self.rand_double_click()
            else:
                self.rand_input()

if __name__ == '__main__':
    os.startfile("C:\Program Files\Google\Chrome\Application\chrome.exe")
    time.sleep(2)

    monkey = MonkeySimple()
    monkey.start(1)

    print("Monkey执行结束.")
    os.system("taskkill -f -IM chrome.exe")

